package view;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import model.TaarifModel;


	/**
	 * This is the BuyaSubscriptionGUI GUI extends AbstractGUI 
	 * @author Tal Panika	
	 */ 
	public class BuyaSubscriptionGUI  extends AbstractGUI
	{
		
		private JLabel priceLabel = null;
		private JLabel lblSubscriptionType=null;
		private JTextField priceTextField=null;
		private JComboBox sCombo=null;
		private JComboBox Combo=null;
		private JButton purchaseButton = null;	
		private JPanel contentPane=null;
		private JLabel wrnMessage=null;
		private JComboBox pcombo=null;
		private String []Location={"Alexandria,Ejypt","Kiryat yam, Israel","Rio,Brazil","Washington DC ,USA","Moscow,Russia","Missisipy,USA","Tripoly,Lybia","Malborn,Australia","California,USA","Vankuver,Canada","Greenland","Paris,France","Madrid,Spain","Toronto , Canada","Buenos Aeres , Argentina","La Pas , Bolivia","Shanghai , China","Lima ,Peru","Mumbai , India","Khartum , Sudan"};
		ArrayList<TaarifModel> tar=null;
		private JLabel lblLocLabel=null;
	

		/**
		 * This is BuyaSubscriptionGUI constructor.
		 * 
		 */
		public BuyaSubscriptionGUI()
		{ 		  
			
			super();
			
			 //Complain tDetails panel
			   contentPane=new JPanel();	
			   contentPane.setBorder(new TitledBorder(null, "Buy A Subscription", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			   contentPane.setLayout(null);
			   contentPane.setOpaque(true);
			
			   contentPane.add(getPriceLabel());				  
			    contentPane.add(getPriceField());
			    contentPane.add(getSubLabel());
				contentPane.add(getPurchaseButton());
				contentPane.add(getBackButton());
				contentPane.add(getWrnMessage());
		        

				  //Frame
			        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			        this.setTitle("Buy A Subscription Window");
			        this.setContentPane(contentPane);      		        
			        	       
			      
				    this.setLocationRelativeTo(null); //This will center the JFrame in the middle of the screen
			        this.setLocationByPlatform(true);
			        this.setSize(499, 299);         
		               
		  }
			
		/**
		 * This method initializes UID  Label
		 */
		private JLabel getPriceLabel() {
			if (priceLabel == null) {
				priceLabel = new JLabel("Price ");
				priceLabel.setBounds(new Rectangle(289, 50, 64, 17));
				
			}
			return priceLabel;
		}
		
		/**
		 * This method initializes Subscription Label
		 */
		private JLabel getSubLabel() {
			if (lblSubscriptionType == null) {
				lblSubscriptionType = new JLabel("Subscription Type");
		        lblSubscriptionType.setBounds(72, 47, 108, 23);		
			}
			return lblSubscriptionType;
		}
				
		
		/**
		 * This method initializes Location Label
		 */
		private JLabel getLocLabel() { 
		if(	lblLocLabel==null){
			 lblLocLabel = new JLabel("On Location:");
	         lblLocLabel.setBounds(72, 126, 122, 20);
	         lblLocLabel.setVisible(false);
		}
        
    	return lblLocLabel;
		}
		/**
		 * This method initializes taarif ComboBox
		 */
		private JComboBox getCombo() {	
			if (sCombo == null) {	
				sCombo = new JComboBox();
				sCombo.setBounds(new Rectangle(72, 78, 180, 23));		
				
			}
			return sCombo;
		}
		
		/**
		 * This method initializes Area ComboBox
		 */
		private JComboBox getCombo2() {	
			if (pcombo==null)
			{
			   pcombo = new JComboBox();
			   pcombo.setBounds(270, 125, 141, 28);
			   pcombo.setVisible(false);
			   pcombo.addItem("Area Type->");
			   for(int i=0;i<Location.length;i++)
				   pcombo.addItem(Location[i]);
			}
			return pcombo;
		}
		
		/**
		 * This method initializes Price Text Field
		 */
		private JTextField getPriceField() {
			if (priceTextField == null) {
				priceTextField = new JTextField();
				priceTextField.setBounds(286, 78, 125, 20); 
				priceTextField.setColumns(10);
				priceTextField.setVisible(false);
			}
			return priceTextField;
		}
		
		
		
		
		/**
		 * This method initializes Purchase Button	
		 */
		private JButton getPurchaseButton() {
			if (purchaseButton == null) {
				purchaseButton = new JButton();
				purchaseButton.setBounds(new Rectangle(72, 174, 100, 35));
				purchaseButton.setText("Purchase ");
				
			}
			return purchaseButton;
		}
		

		/**
		 * This method creates warning message if currently is null.
		 * @return JLabel type.
		 */
		private JLabel getWrnMessage() {
			if (wrnMessage == null) {
				wrnMessage = new JLabel();
				wrnMessage.setForeground(Color.RED);
				wrnMessage.setBounds(70, 16, 320, 23);
				wrnMessage.setVisible(false);
			}
			return wrnMessage;
		}
		
		/**
		 * This method initializes Back Button	
		 */
		private JButton getBackButton() {
			if (backButton == null) {
				backButton = new JButton("Back to Main");
				backButton.setBounds(250, 174, 150, 35);
			
			}
			return backButton;
		}
		
		
		

		/**
		 * *********************************Public Methods 
		 */
		
		/**
		 * This method display Warning Message.
		 * @param void	 
		 * */
		
		public void displayWrnMessage(String msg){
			wrnMessage.setText(msg);
			wrnMessage.setVisible(true);
		}
	
	
		
		/**
		 * This method add action listener to Purchase button.
		 * @param listener
		 */
		public void addPurchaseActionListener(ActionListener listener){
			purchaseButton.addActionListener(listener);
		}
		
		
		/**
		 * This method set ArrayList of Taarif.
		 * @param ArrayList<Taarif> t
		 */
		public void setArrayTaarif(ArrayList<TaarifModel> t)
		{
			this.tar=new ArrayList<TaarifModel>();
			 for(int i=0;i<t.size();i++)
				 tar.add(new TaarifModel(t.get(i).getserviceID(),t.get(i).getdescription(),t.get(i).getoldPrice(),0));
		}
		
		/**
		 * This method return selected item from combobox .
		 * @param comboBox
		 */
		public int getItemSelectedID()
		{
			 int index;
			    
					index=Combo.getSelectedIndex();
					index--;
					System.out.println("getItemSelectedID: "+index+"::::"+tar.get(index).getserviceID());
				
				return	tar.get(index).getserviceID();
				
		}
		/**
		 * This method return selected item from combobox .
		 * @param comboBox
		 */
		public int getItemSelectedIndex()
		{
			    int index;
					index=Combo.getSelectedIndex();
					
				return	index;
				
		}
		
		/**
		 * This method return selected item from combobox2 .
		 * @param comboBox
		 */
		public int getItemSelectedIndex2()
		{
			    int index;
					index=pcombo.getSelectedIndex();
					
				return	index;
				
		}
			
		/**
		 * This method set data to combo from data retrieve from db and set price to taarif
		 * void
		 */
		
		public void setCombo()
		{	 
			Combo=this.getCombo();
			Combo.removeAllItems();
			
			Combo.setVisible(true);
			Combo.addItem("Subscription Type->");
			
		    
		    
			for(int i=0; i<tar.size();i++)
				Combo.addItem(tar.get(i).getdescription());
			
			Combo.addItemListener(new java.awt.event.ItemListener() 
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
			    {
					int index=-1;
					System.out.println("selected"+sCombo.getSelectedIndex()); 
					
					index=Combo.getSelectedIndex();
					
					if (index>0)
					{
					//  System.out.println(String.valueOf(tar.get(index-1).getprice()));
					  priceTextField.setText(String.valueOf(tar.get(index-1).getoldPrice())+" $");
					  priceTextField.setVisible(true);
					  priceTextField.setEditable(false);
					}
					
					//daily
					if(index==2 && tar.size()==4)
					{
						getCombo2().setVisible(true);
						getLocLabel().setVisible(true);
						
					}
					else
					{
						getCombo2().setVisible(false);
						getLocLabel().setVisible(false);
					}
				}
			});
			
			contentPane.repaint();
			contentPane.validate();
	     contentPane.add(getCombo2());
	     contentPane.add(getLocLabel());
		contentPane.add(Combo);
			
		}
		/**
		 * This method dispose Combo
		 * @param void
		 */
		
		public void disposeCombo()
		{
			if(Combo!=null)
			{
				Combo.setVisible(false);
				Combo.removeAllItems();
				Combo = null;
				
			}
				
		}
		/**
		 * Clear fields.
		 */
		public void clearFields(){
			this.getPriceField().setText("");
			
		}
}
